/**
 * 
 */
package org.vsg.stock.basic.persistence.mybatis;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.vsg.stock.basic.domain.BalanceLineKeyPoint;
import org.vsg.stock.basic.persistence.BalanceLineKeypointDao;

/**
 * @author Bill Vison
 *
 */
public class BalanceLineKeypointDaoImpl extends SqlSessionDaoSupport implements BalanceLineKeypointDao {

	private static final Logger logger = LoggerFactory.getLogger(BalanceLineKeypointDaoImpl.class);
	
	
	static private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");		

	@Override
	public void save(BalanceLineKeyPoint keyPoint) throws DataAccessException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<BalanceLineKeyPoint> findByStock(String stockCode)
			throws DataAccessException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BalanceLineKeyPoint getDifferenceFromCloselyPriceByLineId(
			String lineId, double diffPrice) throws DataAccessException {
		String statement = "org.vsg.stock.basic.domain.mybatis.BalanceLineKeyPointProxy." +
		"getDifferenceFromCloselyPriceByLineId";
		
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("lineId", lineId);
		params.put("differenceFromPrice", diffPrice);
		
		Map resultMap = (Map)getSqlSessionTemplate().selectOne(statement , params);
		
		BalanceLineKeyPoint point = null;
		if (resultMap != null) {
			
			point = new BalanceLineKeyPoint();
			point.setPointId( resultMap.get("point_id").toString()  );
			point.setStockCode( resultMap.get("stock_code").toString() );
			point.setPrice( new Double(resultMap.get("price").toString()) );
			point.setSubstanceFlag(  new Short(resultMap.get("substance_flag").toString()) );
			point.setIntegerFlag( new Short(resultMap.get("integer_flag").toString()) );
			point.setPriceType(  resultMap.get("price_type").toString() );

			try {
				point.setRefDate( df.parse( resultMap.get("ref_date").toString() ));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		
		return point;
	}

	@Override
	public double getAveragePriceByLineId(String lineId)
			throws DataAccessException {
		String statement = "org.vsg.stock.basic.domain.mybatis.BalanceLineKeyPointProxy." +
		"getAveragePriceByLineId";
		
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("lineId", lineId);
		Map resultMap = (Map)getSqlSessionTemplate().selectOne(statement , params);

		return new Double(resultMap.get("avgprice").toString());
	}
		
	

	
	

	
	
}
